//
//  TTNoResultManager.m
//  ExhibitionHall
//
//  Created by 王洋 on 2019/7/8.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "TTNoResultView.h"
#import "TTNoResultManager.h"

@interface TTNoResultManager ()

@property (nonatomic, strong) NSMapTable *resultMapTable;

@end

@implementation TTNoResultManager

+(instancetype)shareManager {
    static TTNoResultManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [[TTNoResultManager alloc] init];
        }
    });
    return manager;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _resultMapTable = [NSMapTable weakToWeakObjectsMapTable];
    }
    return self;
}

- (void)showNoResultWithStateInView:(UIView *)view
                          errorCode:(NSInteger)code
                           btnTitle:(NSString *)btnTitle
                     didPressButton:(void (^)(void))didPressButton {
    NSString *message = nil;
    NSString *buttonTitle = nil;
    NSString *imageName = nil;
    
    if (code == kTTViewStateNoData) {
        message = @"空空如也~";
        imageName = @"no_data";
    }else if (code == kTTViewStateNetError) {
        message = @"网络不给力，请刷新页面~";
        buttonTitle = @"重新加载";
        imageName = @"no_wifi";
    }else if (code == kTTViewStateSearchEmpty){
        message = @"没有查询到相关结果，先看看其他的吧~";
        buttonTitle = @"换个词搜索";
        imageName = @"no_data";
    }else if(code == kTTViewStateTimeOut) {
        message = @"请求超时，请重试~";
        buttonTitle = @"再试试";
        imageName = @"time_out";
    }else if (code == kTTViewStateAddressEmpty) {
        message = @"暂时还没有填写地址";
        buttonTitle = @"";
        imageName = @"noAddress";
    }
    ///////////////////////这里是为了适配网络组件//////////////////////////
    else if (code == kCFURLErrorTimedOut) {//-1001
        message = @"请求超时，请重试~";
        buttonTitle = @"再试试";
        imageName = @"time_out";
    }else if (code == kCFURLErrorNotConnectedToInternet) {//-1009
        message = @"您的网络不稳定，请刷新重试~";
        buttonTitle = @"重新加载";
        imageName = @"no_wifi";
    }
    ///////////////////////这里是为了适配网络组件//////////////////////////
    
    else {
        message = @"服务器异常，请联系程序服务商";
        imageName = @"server_error";
    }
    if (btnTitle.length) {
        buttonTitle = btnTitle;
    }
    TTNoResultView *resultView = [TTNoResultView noResultInstance];
    [resultView showNoResultInView:view
                         imageName:imageName
                           message:message
                          btnTitle:buttonTitle
                    didPressButton:didPressButton];
    TTNoResultView *oldView = [_resultMapTable objectForKey:view];
    if (oldView) {
        [oldView hideNoResult];
    }
    [_resultMapTable setObject:resultView forKey:view?:@"view"];
}


- (void)hideNoResultWithView:(UIView*)view {
    if (view) {
        TTNoResultView *resultView = [_resultMapTable objectForKey:view];
        [resultView hideNoResult];
        [_resultMapTable removeObjectForKey:view];
    }
}

@end
